(* $Id: PFOLTypesEncoding.ml 121 2007-10-23 13:24:53Z yann.regisgianas $ *)

(* Pangolin, a functional programming language for correct programs.
   Copyright (C) 2007, Yann Régis-Gianas, François Pottier.
   Contact: yann.regisgianas@gmail.com

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. *)

open PFOLSyntax
module X = XCoreSyntax
open XCoreSyntaxExt
open PFOLEnv

let encode_xprimitive_type_as_primitive_type = function
  | X.TInt  -> TInt
  | X.TBool -> TBool
  | X.TUnit -> TUnit

let rec encode_term_type_as_term_type env = function
  | X.TVar x ->
      TVar (lookup_type_variable env x)

  | X.TApp (tycon, tys) ->
      TApp (lookup_type_variable env tycon,
	    List.map (encode_term_type_as_term_type env) tys)

  | X.TPrimitive p ->
      TPrimitive (encode_xprimitive_type_as_primitive_type p)

  | X.TArrow (ins, outs) ->
      TCArrow (encode_term_type_as_term_type env ins,
	       encode_term_type_as_term_type env outs)

  | X.TProd ts ->
      TProd (List.map (encode_term_type_as_term_type env) ts)


let rec encode_formula_type_as_term_type env = function
  | X.FTVar x ->
      TVar (lookup_type_variable env x)

  | X.FTApp (tycon, tys) ->
      TApp (lookup_type_variable env tycon,
	    List.map (encode_formula_type_as_term_type env) tys)

  | X.FTPrimitive p ->
      TPrimitive (encode_xprimitive_type_as_primitive_type p)

  | X.FTCArrow (ins, outs) ->
      TCArrow (encode_formula_type_as_term_type env ins,
	       encode_formula_type_as_term_type env outs)
	
	
  | X.FTArrow (ins, outs) ->
      TArrow (encode_formula_type_as_term_type env ins,
	      encode_formula_type_as_term_type env outs)


  | X.FTProd ts ->
      TProd (List.map (encode_formula_type_as_term_type env) ts)

  | X.FTProp ->
      TProp

let rec is_prop_return = function
  | X.FTProp -> true
  | X.FTArrow (_, outs) -> is_prop_return outs
  | _ -> false

let rec encode_formula_type_as_object_term_type env = function
  | X.FTVar x ->
      TVar (lookup_type_variable env x)

  | X.FTApp (tycon, tys) ->
      TApp (lookup_type_variable env tycon,
	    List.map (encode_formula_type_as_object_term_type env) tys)

  | X.FTPrimitive p ->
      TPrimitive (encode_xprimitive_type_as_primitive_type p)

  | X.FTCArrow (ins, outs) ->
      TCArrow (encode_formula_type_as_object_term_type env ins,
	       encode_formula_type_as_object_term_type env outs)
	
	
  | X.FTArrow (ins, outs) when is_prop_return outs ->
      TPArrow (encode_formula_type_as_object_term_type env ins,
	       encode_formula_type_as_object_term_type env outs)

  | X.FTArrow (ins, outs) ->
      TArrow (encode_formula_type_as_object_term_type env ins,
	      encode_formula_type_as_object_term_type env outs)


  | X.FTProd ts ->
      TProd (List.map (encode_formula_type_as_object_term_type env) ts)

  | X.FTProp ->
      TProp

let rec encode_formula_type_as_predicate_type env = function
  | X.FTArrow (X.FTProd ins, X.FTProp) -> 
      List.map (encode_formula_type_as_term_type env) ins

  | X.FTArrow (ins, X.FTProp) -> 
      [ encode_formula_type_as_term_type env ins ]

  | X.FTProp -> 
      []

  | _ ->
      assert false


let encode_formula_type_scheme_as_predicate_type_scheme pos env 
    (X.FTScheme abs) = 
  let (vs, ty) = X.open_formula_scheme_abs abs in
  let tvs = List.map (fun v -> (v, X.KStar)) vs in
  let env = introduce_rigid_type_variables pos env tvs in
    create_predicate_type_scheme_abs 
      (type_parameters env vs, 
       encode_formula_type_as_predicate_type env ty)

let encode_formula_type_scheme_as_type_scheme pos env (X.FTScheme abs) = 
  let (vs, ty) = X.open_formula_scheme_abs abs in
  let tvs = List.map (fun v -> (v, X.KStar)) vs in
  let env = introduce_rigid_type_variables pos env tvs in
    create_type_scheme_abs 
      (type_parameters env vs, 
       encode_formula_type_as_term_type env ty)

let encode_formula_type_scheme_as_object_type_scheme pos env (X.FTScheme abs) = 
  let (vs, ty) = X.open_formula_scheme_abs abs in
  let tvs = List.map (fun v -> (v, X.KStar)) vs in
  let env = introduce_rigid_type_variables pos env tvs in
    create_type_scheme_abs 
      (type_parameters env vs, 
       encode_formula_type_as_object_term_type env ty)

let encode_xtype_scheme_as_type_scheme pos env (X.TScheme abs) = 
  let (vs, ty) = X.open_scheme_abs abs in
  let tvs = List.map (fun v -> (v, X.KStar)) vs in
  let env = introduce_rigid_type_variables pos env tvs in
    create_type_scheme_abs 
      (type_parameters env vs, 
       encode_term_type_as_term_type env ty)


